package com.glory.gloryquickui.view

import android.content.Context
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.glory.gloryquickui.R
import com.glory.gloryquickui.adapter.CheckboxSelectAdapter
import com.glory.gloryquickui.entity.FieldBean
import com.glory.gloryquickui.entity.SingleChooseOptionsBean
import com.glory.gloryquickui.utils.FloatingWindowsUtils
import com.glory.gloryquickui.viewmodel.QuickUIViewModel

/**
 *多选框
 */
class CheckBoxSelectController(context: Context?, val viewModel: QuickUIViewModel) :
    ViewController<FieldBean?>(context) {
    private var tvCheckboxSelectName: TextView? = null
    private var rvCheckbox: RecyclerView? = null
    private var mAdapter: CheckboxSelectAdapter? = null
    private var optionList:ArrayList<SingleChooseOptionsBean.Option> = arrayListOf()
    override fun resLayoutId(): Int {
        return R.layout.layout_checkbox_select
    }

    override fun onBindView(data: FieldBean?) {
        tvCheckboxSelectName!!.text = data?.config?.label
        val mLayoutManager = GridLayoutManager(context, 3)
        rvCheckbox!!.layoutManager = mLayoutManager
        setAdapter(data)
    }

    private fun setAdapter(data: FieldBean?) {
        if(data?.config?.option?.dataType == "manual") {
            optionList.clear()
            for (option in data?.config?.option!!.optionList) {
                if (data?.config?.option?.dependObject!!.isEmpty()) {
                    optionList.add(option)
                }
            }
            updateCheckBoxView()
        }else if (data?.config?.option?.dataType == "dictionary"){
            viewModel.searchDictionaryInfo(data)
        }else if (data?.config?.option?.dataType == "dataTable"){
            viewModel.searchDataTableInfo(data)
        }else if (data?.config?.option?.dataType == "third"){
            viewModel.searchDataThirdInfo(data)
        }
    }

    fun updateCheckBoxData(dataList:ArrayList<SingleChooseOptionsBean.Option>){
        this.optionList = dataList
        updateCheckBoxView()
    }

    fun updateCheckBoxView(){
        mAdapter = CheckboxSelectAdapter(
            optionList,
            object : FloatingWindowsUtils.PopTextAndPositionCallback {
                override fun onSuccess(string: String?, potion: Int) {
                    var str = ""
                    optionList.forEach {
                        if (it.isSelect) {
                            str += it.value + ";"
                        }
                    }
                    val map = viewModel.pageData.value!!.toMutableMap()
                    map[data!!.vModel] = str
                    viewModel.pageData.value = map
                }
            }, data!!.disabled, data!!.config.border
        )
        rvCheckbox!!.adapter = mAdapter
    }


    override fun onCreatedView(view: View?) {
        tvCheckboxSelectName = view?.findViewById(R.id.tvCheckboxSelectName)
        rvCheckbox = view?.findViewById(R.id.rvCheckbox)
    }

    override fun masterPart(): View? {
        return rvCheckbox
    }
}